package com.cn.serverline.lineicp.productionLine.handler;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cn.serverline.lineicp.productionLine.entity.ProcessMaterials;
import com.cn.serverline.lineicp.workshopPlan.entity.TypesOfConsumables;
import com.cn.serverline.lineicp.workshopPlan.mapper.TypesOfConsumablesMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * ConsumablesHandler 类，实现了处理耗材的具体操作
 *
 * @author Gmr
 * @since 2024/8/1
 */
@Component
public class ConsumablesHandler implements MaterialHandler {

	@Resource
	private TypesOfConsumablesMapper consumablesMapper;

	@Override
	public void saveMaterial(ProcessMaterials processMaterials) {
		TypesOfConsumables consumables = new TypesOfConsumables();
		BeanUtil.copyProperties(processMaterials, consumables);
		consumablesMapper.insert(consumables);
		processMaterials.setMaterialTypeId(consumables.getId());
	}

	@Override
	public boolean validateMaterial(String code) {
		return consumablesMapper.selectCount(Wrappers.<TypesOfConsumables>lambdaQuery()
				.eq(TypesOfConsumables::getCode, code)) > 0;
	}

	@Override
	public Long getMaterialTypeId(String code) {
		return consumablesMapper.selectOne(Wrappers.<TypesOfConsumables>lambdaQuery()
				.eq(TypesOfConsumables::getCode, code)).getId();
	}
}
